<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/2/2
 * Time: 8:51
 */

namespace app\jiangnan\controller;


use app\jiangnan\config\JNConstants;
use app\service\cxyunService;
use app\utils\JwtUtils;
use think\Controller;
use think\Db;
use think\Log;

class CxyunCar extends Controller{
    public function getListByPlateNum(){
        $plateNumber=request()->get("plateNumber");
        $info=Db::table("cxyun_pklot")
            ->where("plateNumber",$plateNumber)
            ->find();
        if(empty($info)){
          return  json(["code"=>500,'msg' => "未查询到车辆信息"]);
        }
        
        $pklot=$info["pklot"];
        $dataList=Db::table("cxyun_pklot")
            ->where("pklot",$pklot)
            ->select();
        return json(["code"=>0,'msg' => '获取成功',"data"=>$dataList,"pklot"=>$pklot]);
    }
    public function add(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $pklot=request()->post("pklot");
        $roominfo=Db::table("roominfo")
            ->where("pklot",$pklot)
            ->find();
        if(!empty($roominfo)){
            return json(["code"=>500,'msg' => '该车位组已经同步，请联系管理员']);
        }
        $dataList=Db::table("cxyun_pklot")
            ->where("pklot",$pklot)
            ->select();
        foreach ($dataList as $value){

            $res=Db::table("cxyun_car")->where("plateNumber",$value["plateNumber"])->find();
            if(!empty($res)){
                return  json(["code"=>500,'msg' => '车牌重复添加']);
            }
        }
        //
        $array=Db::table("cxyun_parking_gate")
            ->where("parkingId",JNConstants::PARKING_ID)
            ->where("isMonthlyStatus",1)
            ->select();
        $gateArr=array_column($array, 'gateId');
        $notData["gateId"]=implode(",",$gateArr);
        $notData["pklot"]=$pklot;
        $notData["familyAddress"]=$roominfo["community_name"].$roominfo["group"].$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];

        //Db::transaction();

        $add["parkingId"]=JNConstants::PARKING_ID;
        $add["carTypeId"]=JNConstants::CAR_MONTHLY_RENTAL;
        //$add["carModelId"]=JNConstants::CAR_MODEL_ID;
        $add["carModelId"]=JNConstants::CAR_MONTHLY_MODEL_ID;

        //$add["pklot"]=$pklot;
        Db::startTrans();
        foreach ($dataList as $value){
            $add["plateNumber"]=$value["plateNumber"];
            $add["empName"]=$value["empName"];
            $add["empMoblie"]=$value["empMoblie"];
            $add["startTime"]="2024-01-01 00:00:00";
            $oneYearLater = strtotime("+3 year");
            $add["endTime"]="2027-01-01 00:00:00";
            $add["timestamp"]=time()."000";
            $cxyunApi=new cxyunService();
            $res=$cxyunApi->commonPost("saveMonthlyRent",$add,$notData);
            $result= json_decode($res,true);
            if($result["status"]==1){

            }else{
                Db::rollback();
                return json(["code"=>500,'msg' => $result["message"],"data"=>$add]);
            }
            $add["room_id"]=$userInfo["room_id"];
            $add["parkingNo"]=JNConstants::PARKING_NO;
            $add["pklot"]=$pklot;
            $add["carType"]=1;//月租车
            unset($add["timestamp"]);
            Db::table("cxyun_car")->insert($add);
            unset($add["room_id"]);
            unset($add["parkingNo"]);
            unset($add["pklot"]);
            unset($add["carType"]);
        }
        Db::table("roominfo")
            ->where("id",$userInfo["room_id"])
            ->update(["pklot"=>$pklot]);
        Db::commit();
        return  json(["code"=>0,'msg' => '添加成功']);
    }
}